<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>模板管理</title>
    <link rel="stylesheet" href="/static/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/static/assets/module/admin.css?v=318"/>
    <link rel="stylesheet" href="/static/assets/css/table.css"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        #templateGroupTable + .layui-table-view .layui-table-tool-temp {
            padding-right: 0;
        }

        #templateGroupTable + .layui-table-view .layui-table-body tbody > tr td {
            cursor: pointer;
        }

        #templateGroupTable + .layui-table-view .layui-table-body tbody > tr.layui-table-click {
            background-color: #fff3e0;
        }

        #templateGroupTable + .layui-table-view .layui-table-body tbody > tr.layui-table-click td:last-child > div:before {
            position: absolute;
            right: 6px;
            content: "\e602";
            font-size: 12px;
            font-style: normal;
            font-family: layui-icon !important;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
        }
        #templateGroupForm .layui-form-label {
            width: 100px;  /* 这里修改标题宽度 */
        }

        #templateGroupForm .layui-input-block {
            margin-left: 130px;  /* 这里要比上面始终大30px */
        }
        #templateEditForm .layui-form-label {
            width: 100px;  /* 这里修改标题宽度 */
        }

        #templateEditForm .layui-input-block {
            margin-left: 130px;  /* 这里要比上面始终大30px */
        }
    </style>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid" style="padding-bottom: 0;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <div class="layui-card">
                <div class="layui-card-body">
                    <!-- 数据表格1 -->
                    <table id="templateGroupTable" lay-filter="templateGroupTable"></table>
                </div>
            </div>
        </div>
        <div class="layui-col-md9">
            <div class="layui-card">
                <div class="layui-card-body">
                    <!-- 表格工具栏2 -->
                    <form class="layui-form search-box">
                            <div class="layui-inline">
                                <label class="layui-form-label">模板组名称:</label>
                                <div class="layui-input-inline">
                                    <input name="dictDataName" class="layui-input" placeholder="输入模版组名称"/>
                                </div>
                            </div>
                            <div class="layui-inline">&emsp;
                                <button class="layui-btn icon-btn" lay-filter="templateTbSearch" lay-submit>
                                    <i class="layui-icon">&#xe615;</i>搜索
                                </button>
                            </div>
                    </form>
                </div>
            </div>
            <div class="layui-card">
                <div class="layui-card-body">
                    <!-- 数据表格2 -->
                    <table id="templateTable" lay-filter="templateTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 表单弹窗1 -->
<script type="text/html" id="templateGroupEditDialog">
    <form id="templateGroupForm" lay-filter="templateGroupEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">模版组名称:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入模版组名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="templateGroupEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="templateTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="editTpl">编辑模板</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="templateEditDialog">
    <form id="templateEditForm" lay-filter="templateEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required" >模板名称:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入模板名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">生成文件名:</label>
            <div class="layui-input-block">
                <input name="fileName" placeholder="请输入生成文件名" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
                <div class="layui-word-aux">实体用{}占位符,例:{}DO.java</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">包后缀:</label>
            <div class="layui-input-block">
                <input name="packageSuffix" placeholder="请输入包后缀" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
                <div class="layui-word-aux">多层用.隔开,例:mapper.xml</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="templateEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script type="text/javascript" src="/static/assets/libs/jquery/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="/static/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="/static/assets/js/common.js?v=318"></script>
<script type="text/javascript" src="/static/assets/js/ajaxUtil.js?v=318"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var selObj;  // 左表选中数据

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#templateGroupTable',
            url: '/template/group/',
            height: 'full-100',
            skin: 'line',
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>新建</button>&nbsp;',
                '<button lay-event="edit" class="layui-btn layui-btn-sm layui-btn-warm icon-btn"><i class="layui-icon">&#xe642;</i>修改</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            defaultToolbar: [],
            cols: [
                [
                {type: 'numbers'},
                {field: 'name', title: '模板组名称'}
            ]
            ],
            done: function (res, curr, count) {
                $('#templateGroupTable+.layui-table-view .layui-table-body tbody>tr:first').trigger('click');
            }
        });

        /* 表格搜索 */
        form.on('submit(templateTbSearch)', function (data) {
            insTb.reload({where: data.field});
            return false;
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(templateGroupTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'edit') { // 修改
                showEditModel(selObj.data, selObj);
            } else if (obj.event === 'del') { // 删除
                doDel(selObj);
            }
        });

        /* 监听行单击事件 */
        table.on('row(templateGroupTable)', function (obj) {
            selObj = obj;
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            insTb2.reload({where: {}, page: {curr: 1}, url: '/template/group/'+obj.data.id});
        });

        /* 显示表单弹窗 */
        function showEditModel(mData, obj) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '模板组',
                content: $('#templateGroupEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('templateGroupEditForm', mData);
                    // 表单提交事件
                    form.on('submit(templateGroupEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.ajax({
                            url: '/template/group/',
                            data: JSON.stringify(data.field),
                            dataType: 'json',
                            method: mData ? 'PUT' : 'POST',
                            contentType: 'application/json;charset=utf-8',
                            success: function (res) {
                                layer.close(loadIndex);
                                if (0 === res.code) {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1});
                                    if (obj) {
                                        obj.update(data.field);
                                    } else {
                                        insTb.reload();
                                    }
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除此模版吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                B.del({
                    url:'/template/group/'+obj.data.id,
                    success:function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({page: {curr: 1}});
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }
                });
            });
        }

        /* 渲染表格2 */
        var insTb2 = table.render({
            elem: '#templateTable',
            data: [],
            height: 'full-135',
            page: true,
            skin: 'line',
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>&nbsp;',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [
                [
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'name', title: '模板名称', sort: true},
                {field: 'fileName', title: '生成文件名', sort: true},
                {field: 'packageSuffix', title: '包后缀', sort: true},
                {field: 'remark', title: '备注', sort: true},
               /* {
                    field: 'createTime', title: '创建时间', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, sort: true
                },*/
                {title: '操作', toolbar: '#templateTbBar', align: 'center', width: 200, minWidth: 200}
            ]
            ]
        });

        /* 表格2搜索 */
        form.on('submit(dictDataTbSearch)', function (data) {
            insTb2.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格2工具条点击事件 */
        table.on('tool(templateTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel2(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel2(obj);
            }else if (obj.event === 'editTpl') {
                codeEdit(obj.data);
            }
        });

        /* 表格2头工具栏点击事件 */
        table.on('toolbar(templateTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel2();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('templateTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.dictDataId;
                });
                doDel2({ids: ids});
            }
        });

        /* 显示表单弹窗2 */
        function showEditModel2(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '模板',
                content: $('#templateEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('templateEditForm', mData);
                    // 表单提交事件
                    form.on('submit(templateEditSubmit)', function (data) {
                        data.field.groupId = selObj.data.id;
                        var loadIndex = layer.load(2);
                        $.ajax({
                            url: '/template/',
                            data: JSON.stringify(data.field),
                            dataType: 'json',
                            method: mData ? 'PUT' : 'POST',
                            contentType: 'application/json;charset=utf-8',
                            success: function (res) {
                                layer.close(loadIndex);
                                if (0 === res.code) {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1});
                                    insTb2.reload();
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        }

        /* 删除2 */
        function doDel2(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                B.del({
                    url:'/template/'+obj.data.id,
                    success:function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            insTb2.reload({page: {curr: 1}});
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }
                });
                /*$.get('../../json/ok.json', {
                    id: obj.data ? obj.data.dictDataId : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (200 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        insTb2.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');*/
            });
        }
        function codeEdit(data) {
            var layIndex = admin.open({
                type: 2,
                title: '在线编辑',
                content: '/page/template/edit',
                offset: '20px',
                maxmin: true,
                data: { template: data },  // 使用data参数传值给弹窗页面
                area: ['80%', '95%'],
                end: function () {
                    if (admin.getLayerData(layIndex, 'formOk')) {  // 判断表单操作成功标识
                        insTb2.reload();  // 成功刷新表格
                    }
                }
            });
        }
    });
</script>
</body>
</html>
